package com.epfresh.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.bean.BaseSpellGoodsBean;
import com.epfresh.bean.NewHomeBean;

import java.util.ArrayList;
import java.util.List;

import multitype.ItemViewBinder;
import multitype.MultiTypeAdapter;

/**
 * Author: Chen
 * Date: 2019/4/23
 * Desc:
 */
public class BaseSpellGoodsViewBinder extends ItemViewBinder<BaseSpellGoodsBean, BaseSpellGoodsViewBinder.ViewHolder> {
    private boolean isInit = true;
    private OnItemClickListener onItemClickListener;

    public void setInit(boolean init) {
        isInit = init;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_base_spell_goods, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final BaseSpellGoodsBean item) {
        if (isInit) {
            holder.items.clear();
            holder.items.addAll(item.getSpellsBeanList());
            if (item.getSpellsBeanList().size() > 1) {
                holder.items.add("共"+item.getSpellTotal()+"件");
            }
            holder.adapter.notifyDataSetChanged();
            holder.txt_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onMoreClick(item);
                    }
                }
            });
            holder.showMoreGoodViewBinder.setOnShowMoreClickListener(new ShowMoreGoodViewBinder.onShowMoreClickListener() {
                @Override
                public void showMoreClick() {
                    if (onItemClickListener != null) {
                        onItemClickListener.onMoreClick(item);
                    }
                }
            });

            isInit = false;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView txt_more;
        private RecyclerView rv_goods_list;
        private BaseSpellGoodsAdapter viewBinderGood;
        private ShowMoreGoodViewBinder showMoreGoodViewBinder;
        private MultiTypeAdapter adapter;
        private List<Object> items = new ArrayList<>();

        ViewHolder(View itemView) {
            super(itemView);

            txt_more = itemView.findViewById(R.id.txt_more);
            rv_goods_list = itemView.findViewById(R.id.rv_goods_list);
            viewBinderGood = new BaseSpellGoodsAdapter();
            showMoreGoodViewBinder = new ShowMoreGoodViewBinder();
            adapter = new MultiTypeAdapter();
            rv_goods_list.setLayoutManager(new LinearLayoutManager(itemView.getContext(), RecyclerView.HORIZONTAL, false));
            adapter.register(NewHomeBean.SpellsBean.class,viewBinderGood);
            adapter.register(String.class,showMoreGoodViewBinder);
            adapter.setItems(items);
            rv_goods_list.setAdapter(adapter);
            viewBinderGood.setOnGoodItemClickListener(new BaseSpellGoodsAdapter.OnGoodItemClickListener() {
                @Override
                public void onGoodItemClick(NewHomeBean.SpellsBean spellsBean) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onGoodClick(spellsBean);
                    }
                }
            });

        }

//        private void setGoodsData(List<NewHomeBean.SpellsBean> goods) {
//            viewBinderGood.setGoods(goods);
//        }
    }

    public interface OnItemClickListener {
        void onMoreClick(BaseSpellGoodsBean item);
        void onGoodClick(NewHomeBean.SpellsBean spellsBean);
    }

}
